<template>
  <div v-if="open" class="modal-overlay" @click.self="$emit('close')">
    <div class="modal card">
      <header class="modal__header">
        <h3>登录轴承商城</h3>
        <button class="modal__close" @click="$emit('close')">×</button>
      </header>
      <p class="modal__intro">
        登录后可查看协议价、添加购物车并发起库存核实。示例页面仅模拟流程，不会发送真实请求。
      </p>
      <form class="modal__form" @submit.prevent="handleSubmit">
        <label>
          <span>手机号</span>
          <input v-model="form.phone" type="tel" required maxlength="11" />
        </label>
        <label>
          <span>密码</span>
          <input v-model="form.password" type="password" required />
        </label>
        <button type="submit" class="btn btn--primary">登录</button>
      </form>
      <footer class="modal__footer">
        <span>还没有账号？可在后台通过运营创建或走注册流程。</span>
      </footer>
    </div>
  </div>
</template>

<script setup>
import { reactive, watch } from "vue";

const props = defineProps({
  open: { type: Boolean, default: false },
  defaultPhone: { type: String, default: "" }
});

const emit = defineEmits(["close", "login"]);

const form = reactive({
  phone: "",
  password: ""
});

watch(
  () => props.defaultPhone,
  value => {
    if (value && !form.phone) {
      form.phone = value;
    }
  },
  { immediate: true }
);

const handleSubmit = () => {
  emit("login", { ...form });
  form.phone = "";
  form.password = "";
};
</script>

<style scoped lang="scss">
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(16, 24, 40, 0.45);
  display: grid;
  place-items: center;
  padding: 24px;
  z-index: 2000;
}

.modal {
  max-width: 420px;
  width: 100%;
  padding: 28px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.modal__header {
  display: flex;
  justify-content: space-between;
  align-items: center;

  h3 {
    margin: 0;
  }
}

.modal__close {
  background: none;
  border: none;
  font-size: 26px;
  line-height: 1;
  cursor: pointer;
  color: var(--gray-500);
}

.modal__intro {
  margin: 0;
  font-size: 14px;
  color: var(--gray-500);
}

.modal__form {
  display: flex;
  flex-direction: column;
  gap: 14px;

  label {
    display: flex;
    flex-direction: column;
    gap: 6px;
    font-size: 14px;
    color: var(--gray-700);
  }

  input {
    border: 1px solid var(--gray-200);
    border-radius: 10px;
    padding: 10px 14px;
    font-size: 14px;

    &:focus {
      border-color: var(--primary-color);
      outline: none;
      box-shadow: 0 0 0 3px rgba(0, 82, 217, 0.18);
    }
  }

  .btn {
    margin-top: 6px;
    border: none;
    border-radius: 10px;
    padding: 12px;
    background: var(--primary-color);
    color: #fff;
    font-weight: 600;
  }
}

.modal__footer {
  font-size: 12px;
  color: var(--gray-500);
}
</style>
